import io from 'socket.io-client'
import { store } from '@/store/index.ts'
import { getUserCtx } from '@/utils/tool'

(window as any).$ctx = getUserCtx();

// process.env.REACT_APP_SOCKET_SERVER
export const socket = io(process.env.REACT_APP_SOCKET_SERVER as string);
window.io = socket as any;
(window as any).$ctx = (window as any).$ctx || {id: ''}

socket.on('hall', (res:any) => { 
  console.log(JSON.parse(res.some));
  store.dispatch({type: 'update_hall', payload: JSON.parse(res.some)});
  calcuMsgStatus(JSON.parse(res.some));
});
socket.on((window as any).$ctx.id, (res:any) => { 
  console.log(JSON.parse(res.some));
  store.dispatch({type: 'update_hall', payload: JSON.parse(res.some)});
  calcuMsgStatus(JSON.parse(res.some));
});

function calcuMsgStatus(data: any) {
  for(var key in data) {
    store.dispatch({type: 'UpdateMsgStatus', payload: {id: key, latest: data[key].msg}});
  }
}

console.log(store);
